package com.wlyuan.index.remote.consumer;

import com.wlyuan.index.api.IWaybillIndexService;
import com.wlyuan.index.remote.message.BalanceMessage;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author yuanjie
 */
@Slf4j
@Component
public class BalanceMessageConsumer extends AbstractMessageConsumer<BalanceMessage> {
    @Resource
    private IWaybillIndexService waybillIndexService;

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(
            value = Queues.BALANCE),
            exchange = @Exchange(value = Exchanges.FINANCE),
            key = {"BillRecordCollected", "BillRecordPayed", "FinanceWaybillsChanged"})
    }, concurrency = "4")
    public void consume(String string) {
        if (logger.isDebugEnabled()) {
            logger.debug("收到运单收付款消息: {}", string);
        }
        try {
            this.handle(string, BalanceMessage.class, message -> {
                var waybills = message.getWaybills();
                waybills.forEach(waybillIndexService::index);
            });
        } catch (Exception e) {
            logger.error("处理运单付款消息失败：{}", string, e);
        }
    }
}